﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Quocity.Context;
using Quocity.Entities;
using System.IO;
using System.Data.Linq;
using System.Configuration;

using System.ServiceModel.Activation;
using PlainConcepts.WCF.Extensions.Cache;
using Microsoft.Ajax.Samples;

namespace QuocityService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class QuocityService : IQuocityService
    {
        [WebGet(
               BodyStyle = WebMessageBodyStyle.Wrapped,
               ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/cities"
         )]
        [JSONPBehavior(callback = "callback")]
        public List<City> GetCitiesJson()
        {
            using (DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["QuocityConnectionString"].ConnectionString))
            {
                context.Log = new StringWriter();
                var cities = from city in context.GetTable<City>()
                             select city;
                return new List<City>(cities);
            }
        }

        [WebGet(UriTemplate = "Xml/Cities", ResponseFormat = WebMessageFormat.Xml)]
        public List<City> GetCitiesXml()
        {
            using (DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["QuocityConnectionString"].ConnectionString))
            {
                context.Log = new StringWriter();
                var cities = from city in context.GetTable<City>()
                             select city;
                return new List<City>(cities);
            }
        }

        [UseCache(604800)]
        [WebGet(UriTemplate = "Json/{city}/PointsOfInterest/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "callback")]
        public List<PointOfInterest> GetPointsOfInterestJson(string city, string page, string pageSize)
        {
            return GetPointsOfInterest(city, page, pageSize, String.Empty);
        }

        [UseCache(604800)]
        [WebGet(UriTemplate = "Xml/{city}/PointsOfInterest/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Xml)]
        public List<PointOfInterest> GetPointsOfInterestXml(string city, string page, string pageSize)
        {
            return GetPointsOfInterest(city, page, pageSize, String.Empty);
        }

        [UseCache(604800)]
        [WebGet(UriTemplate = "Json/{city}/PointsOfInterest/{filter}/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "callback")]
        public List<PointOfInterest> GetPointsOfInterestFilteredJson(string city, string page, string pageSize, string filter)
        {
            return GetPointsOfInterest(city, page, pageSize, filter);
        }

        [UseCache(604800)]
        [WebGet(UriTemplate = "Xml/{city}/PointsOfInterest/{filter}/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Xml)]
        public List<PointOfInterest> GetPointsOfInterestFilteredXml(string city, string page, string pageSize, string filter)
        {
            return GetPointsOfInterest(city, page, pageSize, filter);
        }



        private List<PointOfInterest> GetPointsOfInterest(string city, string page, string pageSize, string filter)
        {
            IPointOfInterestSource source = new XmlPointOfInterestSource(city);
            return source.GetList(filter, Int32.Parse(page), Int32.Parse(pageSize));
        }

        [UseCache(30)]
        [WebGet(UriTemplate = "Json/{city}/Itineraries/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "callback")]
        public List<Itinerary> GetItinerariesJson(string city, string page, string pagesize)
        {
            return GetItineraries(city, page, page);
        }


        [UseCache(30)]
        [WebGet(UriTemplate = "Xml/{city}/Itineraries/{page}/{pagesize}", ResponseFormat = WebMessageFormat.Xml)]
        public List<Itinerary> GetItinerariesXml(string city, string page, string pagesize)
        {
            return GetItineraries(city, page, page);
        }

        public List<Itinerary> GetItineraries(string city, string page, string pagesize)
        {
            using (DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["QuocityConnectionString"].ConnectionString))
            {
                DataLoadOptions loadOptions = new DataLoadOptions();
                loadOptions.LoadWith<Itinerary>(itinerary => itinerary.City);
                context.LoadOptions = loadOptions;
                var itineraries = from itinerary in context.GetTable<Itinerary>()
                                  where String.Compare(itinerary.City.Name, city, true) == 0
                                  select itinerary;
                return new List<Itinerary>(itineraries);
            }
        }


        [UseCache(30)]
        [WebGet(UriTemplate = "Json/Itinerary/{itineraryId}", ResponseFormat = WebMessageFormat.Json)]
        [JSONPBehavior(callback = "callback")]
        public Itinerary GetItineraryJson(string itineraryId)
        {
            return GetItinerary(itineraryId);
        }

        [UseCache(30)]
        [WebGet(UriTemplate = "Xml/Itinerary/{itineraryId}", ResponseFormat = WebMessageFormat.Xml)]
        public Itinerary GetItineraryXml(string itineraryId)
        {
            return GetItinerary(itineraryId);
        }

        private Itinerary GetItinerary(string itineraryId)
        {
            int id;
            if (Int32.TryParse(itineraryId, out id))
            {
                using (DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["QuocityConnectionString"].ConnectionString))
                {

                    DataLoadOptions loadOptions = new DataLoadOptions();
                    loadOptions.AssociateWith<Itinerary>(i => i.ItineraryItems.OrderBy(ii => ii.SortIndex));
                    loadOptions.LoadWith<Itinerary>(i => i.ItineraryItems);
                    
                    context.LoadOptions = loadOptions;
                    var itinerary = (from i in context.GetTable<Itinerary>()
                                     where i.ItineraryId == id
                                     select i).FirstOrDefault();
                    if (itinerary != null)
                    {
                        
                        return itinerary;
                        

                    }
                    return null;
                }
                
            }
            return null;
        }
    }
}
